package com.lagou.edu.authority.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.edu.authority.entity.RoleMenu;
import com.lagou.edu.authority.mapper.RoleMenuMapper;
import com.lagou.edu.authority.service.IRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author lm
 * @since 2021/11/10 17:52
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
    @Override
    public Set<Integer> getMenuIdListByRoleIds(Set<Integer> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return null;
        }
        return lambdaQuery()
                .in(RoleMenu::getRoleId, roleIds)
                .list()
                .stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toSet());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRoleMenuIdList(Integer roleId, Set<Integer> needToDelMenuIds) {
        return lambdaUpdate()
                .eq(RoleMenu::getRoleId, roleId)
                .in(RoleMenu::getMenuId, needToDelMenuIds)
                .remove();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeByRoleId(Integer roleId) {
        return lambdaUpdate()
                .eq(RoleMenu::getRoleId, roleId)
                .remove();
    }
}
